﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;

namespace MakeItWeb.Framework
{
    [Serializable]
    public class DatabaseContent : BusinessObject
    {
        int m_iContentId = int.MinValue;
        string m_sContentCd;
        string m_sContentTxt;
        DateTime m_dtCreateDttm;
        DateTime m_dtUpdateDttm;
        string m_sContentHtmlTxt;
        Guid m_guidHtmlPageId;
        bool m_bDisplaySectionheaderInd = false;
        string m_sSectionheaderTxt = string.Empty;
        string m_sDividerColorTxt = string.Empty;
        int m_iPriorityNum;

        #region Public properties

        public int ContentId
        {
            get { return m_iContentId; }
        }
        public string ContentCd
        {
            get { return m_sContentCd; }
            set
            {
                if (!Loaded)
                {
                    m_sContentCd = value;
                    SetDirty();
                }
            }
        }
        public string ContentTxt
        {
            get { return m_sContentTxt; }
            set
            {
                if (!value.Equals(m_sContentTxt))
                {
                    m_sContentTxt = value;
                    SetDirty();
                }
            }
        }
        public DateTime CreateDttm
        {
            get { return m_dtCreateDttm; }
        }
        public DateTime UpdateDttm
        {
            get { return m_dtUpdateDttm; }
        }
        public string ContentHtmlTxt
        {
            get { return m_sContentHtmlTxt; }
            set
            {
                if (!value.Equals(m_sContentHtmlTxt))
                {
                    m_sContentHtmlTxt = value;
                    SetDirty();
                }
            }
        }
        public Guid HtmlPageId
        {
            get { return m_guidHtmlPageId; }
            set
            {
                if (!value.Equals(m_guidHtmlPageId))
                {
                    m_guidHtmlPageId = value;
                    SetDirty();
                }
            }
        }

        public bool DisplaySectionheaderInd
        {
            get { return m_bDisplaySectionheaderInd; }
            set
            {
                if (!value.Equals(m_bDisplaySectionheaderInd))
                {
                    m_bDisplaySectionheaderInd = value;
                    SetDirty();
                }
            }
        }

        public string SectionheaderTxt
        {
            get { return m_sSectionheaderTxt; }
            set
            {
                if (!value.Equals(m_sSectionheaderTxt))
                {
                    m_sSectionheaderTxt = value;
                    SetDirty();
                }
            }
        }

        public string DividerColorTxt
        {
            get { return m_sDividerColorTxt; }
            set
            {
                if (!value.Equals(m_sDividerColorTxt))
                {
                    m_sDividerColorTxt = value;
                    SetDirty();
                }
            }
        }

        public int PriorityNum
        {
            get { return m_iPriorityNum; }
            set
            {
                if (!value.Equals(m_iPriorityNum))
                {
                    m_iPriorityNum = value;
                    SetDirty();
                }
            }
        }

        #endregion Public properties

        public override void Load(System.Data.DataRow dr)
        {
            m_iContentId = (int)dr["content_id"];
            m_sContentCd = (string)dr["content_cd"];
            m_sContentTxt = (string)dr["content_txt"];
            m_dtCreateDttm = (DateTime)dr["create_dttm"];
            m_dtUpdateDttm = (DateTime)dr["update_dttm"];
            m_sContentHtmlTxt = (string)dr["content_html_txt"];
            m_guidHtmlPageId = (Guid)dr["html_page_id"];
            m_bDisplaySectionheaderInd = (bool)dr["display_sectionheader_ind"];
            if (DBNull.Value.Equals(dr["sectionheader_txt"]) == false)
                m_sSectionheaderTxt = (string)dr["sectionheader_txt"];
            if (DBNull.Value.Equals(dr["divider_color_txt"]) == false)
                m_sDividerColorTxt = (string)dr["divider_color_txt"];
            m_iPriorityNum = (int)dr["priority_num"];
            SetLoaded();
        }

        public override void Save()
        {
            if (Dirty)
            {
                StoredProcArgs args = new StoredProcArgs();
                if (int.MinValue.Equals(ContentId) == false)
                    args["pContentId"] = ContentId;
                args["pContentCd"] = ContentCd;
                args["pContentTxt"] = ContentTxt;
                args["pContentHtmlTxt"] = ContentHtmlTxt;
                args["pHtmlPageId"] = HtmlPageId;
                args["pDisplaySectionheaderInd"] = DisplaySectionheaderInd;
                if (string.IsNullOrEmpty(SectionheaderTxt) == false)
                    args["pSectionheaderTxt"] = SectionheaderTxt;
                if (string.IsNullOrEmpty(DividerColorTxt) == false)
                    args["pDividerColorTxt"] = DividerColorTxt;
                args["pPriorityNum"] = PriorityNum;
                GenericCud.ExecuteCud("proc_cm_SaveDatabaseContent", args);

                SetDirty(false);
            }
        }

        public static DatabaseContent GetDatabaseContentByCd(string sContentCd, Guid guidHtmlPageId)
        {
            return GetDatabaseContentByCd(sContentCd, guidHtmlPageId, true);
        }

        public static DatabaseContent GetDatabaseContentByCd(string sContentCd, Guid guidHtmlPageId, bool bCreateIfNotExists)
        {
            DatabaseContent oContent = new DatabaseContent();
            StoredProcArgs args = new StoredProcArgs();
            args["pContentCd"] = sContentCd;
            args["pHtmlPageId"] = guidHtmlPageId;
            args["pCreateIfNotExists"] = bCreateIfNotExists;
            using (DataSet oDs = GenericQry.GetDataSet("proc_cm_GetContentByCode", args))
            {
                if (oDs.Tables[0].Rows.Count > 0)
                    oContent.Load(oDs.Tables[0].Rows[0]);
            }
            return oContent;
        }

        public static DatabaseContent GetDatabaseContentByCd(string sContentCd)
        {
            DatabaseContent oContent = new DatabaseContent();
            StoredProcArgs args = new StoredProcArgs();
            args["pContentCd"] = sContentCd;
            using (DataSet oDs = GenericQry.GetDataSet("proc_cm_GetContentByContentCode", args))
            {
                if (oDs.Tables[0].Rows.Count > 0)
                    oContent.Load(oDs.Tables[0].Rows[0]);
            }
            return oContent;
        }

        public static DatabaseContent[] ListContentByHtmlPageId(Guid guidHtmlPageId)
        {
            return ListContentByHtmlPageId(guidHtmlPageId, false);
        }

        public static DatabaseContent[] ListContentByHtmlPageId(Guid guidHtmlPageId, bool bFlexibleContent)
        {
            List<DatabaseContent> lstContent = new List<DatabaseContent>();
            StoredProcArgs args = new StoredProcArgs();
            args["pHtmlPageId"] = guidHtmlPageId;
            using (DataSet ds = GenericQry.GetDataSet("proc_cm_ListContentByHtmlPageId", args))
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DatabaseContent dc = new DatabaseContent();
                    dc.Load(dr);
                    try
                    {
                        Guid guid = Guid.Empty;
                        if (!bFlexibleContent)
                            guid = new Guid(dc.ContentCd);
                        lstContent.Add(dc);
                    }
                    catch (Exception)
                    {
                        // ignore this one.
                    }
                }
            }
            return lstContent.ToArray();
        }

        public static void DeleteDatabaseContentByCd(string sContentCode)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pContentCd"] = sContentCode;
            GenericCud.ExecuteCud("proc_cm_DeleteDatabaseContentByCd", args);
        }

        public static void MoveDownDatabaseContentByCd(string sContentCode)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pContentCd"] = sContentCode;
            GenericCud.ExecuteCud("proc_cm_MoveDownDatabaseContentByCd", args);
        }

        public static void MoveUpDatabaseContentByCd(string sContentCode)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pContentCd"] = sContentCode;
            GenericCud.ExecuteCud("proc_cm_MoveUpDatabaseContentByCd", args);
        }

        public static DatabaseContent[] ListFlexiblePageContent()
        {
            return ListContentByHtmlPageId(new Guid(ConfigurationManager.AppSettings["GenericPageId"]), true);
        }

        public static void DeleteDatabaseContentById(int iContentId)
        {
            StoredProcArgs args = new StoredProcArgs();
            args["pContentId"] = iContentId;
            GenericCud.ExecuteCud("proc_cm_DeleteDatabaseContentById", args);
        }
    }
}